﻿package component.fileupload.action;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import reget.SiteFileFetch;
import reget.SiteInfoBean;

import component.fileupload.exception.FileUploadException;
import component.fileupload.util.FileUtil;
import component.fileupload.util.HttpDownloadUtil;

/**
 * 文件下载的实现类
 */
public class FileDownloadAction implements IFileDownloadAction {
	private String saveFilePath;
	
	public String getSaveFilePath() {
		return saveFilePath;
	}

	public void setSaveFilePath(String saveFilePath) {
		this.saveFilePath = saveFilePath;
	}

	public String doDownload(ServletContext servletContext,
			HttpServletRequest request, HttpServletResponse response,
			String filePath, String fileName) throws FileUploadException {
		filePath = FileUtil.changeSlash(filePath);

		String realPath = servletContext.getRealPath("");
		realPath = FileUtil.changeSlash(realPath);

		// Properties prop = new Properties(System.getProperties());
		// String osName = prop.getProperty("os.name");

		String serverFilePath = realPath + FileUtil.getFileSeparator()
				+ filePath + FileUtil.getFileSeparator() + fileName;
		File file = new File(serverFilePath);

		int index = fileName.lastIndexOf('.');
		String suffixName = fileName.substring(index + 1, fileName.length()); // 后缀名
		
		if ("txt".equals(suffixName)) {
			response.setContentType("text/txt");
		} else if (".htm ".equals(suffixName) || ".html ".equals(suffixName)) {
			response.setContentType("text/html");
		} else if (".doc ".equals(suffixName)) {
			response.setContentType("application/msword");
		} else if (".xls ".equals(suffixName)) {
			response.setContentType("application/msexcel");
		} else if (".ppt ".equals(suffixName)) {
			response.setContentType("application/ms-powerpoint");
		} else if (".pdf ".equals(suffixName)) {
			response.setContentType("application/pdf");
		} else if (".rm ".equals(suffixName) || ".ram ".equals(suffixName)) {
			response.setContentType("audio/x-pn-realaudio");
		} else if (".zip ".equals(suffixName)) {
			response.setContentType("application/zip");
		} else {
			response.setContentType("application/x-msdownload");
			//response.setContentType("application/octet-stream");
			// response.setContentType("application/x-download"); 
		}

		BufferedOutputStream bos = null;
		FileInputStream fis = null;
		try {
			//处理下载的文件名出现乱码的问题
			response.setHeader("Content-Disposition", "attachment;filename="
					+ new String(fileName.getBytes(FileUtil.CHARSET),
							"ISO-8859-1"));
			if (file.exists()) {
				bos = new BufferedOutputStream(response.getOutputStream());
				fis = new FileInputStream(file);
				// 设置每次写入缓存大小
				byte[] buffer = new byte[8012];
				int bytesRead = 0;
				// 把输出流写入客户端
				while ((bytesRead = fis.read(buffer, 0, 8012)) != -1) {
					bos.write(buffer, 0, bytesRead);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new FileUploadException(e);
		} finally {
			try {
				if (fis != null)
					fis.close();
				if (bos != null)
					bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return "true";
	}
	
	public String doDownload(String httpFile, String localFilePath, String saveFileName) throws FileUploadException {
		HttpDownloadUtil.downloadFile(httpFile, localFilePath + FileUtil.getFileSeparator() + saveFileName);
		return "true";
	}
	
	public String doReDownload(String httpFile, String localFilePath, String saveFileName, int breakpoint) throws FileUploadException {
		SiteInfoBean bean = new SiteInfoBean(httpFile, localFilePath, saveFileName, breakpoint);
		SiteFileFetch fileFetch = null;
		try {
			fileFetch = new SiteFileFetch(bean);
			fileFetch.start();
		} catch (IOException e) {
			e.printStackTrace();
			throw new FileUploadException(e);
		}
		
		return "true";
	}

	public static void main(String[] args) {
		String fileName = "a.a.txt";
		int index = fileName.lastIndexOf('.');
		String suffixName = fileName.substring(index + 1, fileName.length()); // 后缀名
		System.out.println(suffixName);
	}

}
